{{- $collapsed := default false .Site.Params.sidebar.collapsed }}
{{- if default true .Site.Params.sidebar.taxonomiesToggle }}
{{- $archives := default true $.Site.Params.sidebar.taxonomiesToggleArchives }}
<div class="accordion taxonomies-toggle">
    <div class="row card component accordion-item">
        <div class="accordion-header card-header border-0">
            <a
                class="accordion-button d-lg-none mb-1 shadow-none p-0 bg-transparent{{ if $collapsed }} collapsed{{ end }}"
                role="button" data-bs-toggle="collapse" href="#taxonomies-toggle" aria-expanded="{{ if $collapsed }}false{{ else }}true{{ end }}" aria-controls="taxonomies-toggle">
                {{ i18n "taxonomies" }}
            </a>
        </div>
        <div class="card-body collapse accordion-collapse accordion-body d-lg-block{{ if not $collapsed }} show{{ end }}" id="taxonomies-toggle">
            <ul class="nav nav-pills nav-fill" role="tablist">
                {{- $counter := 0 }}
                {{- range $expected := $.Site.Params.sidebarTaxonomies }}
                {{- range $key, $value := $.Site.Taxonomies }}
                {{- if eq $key $expected }}
                {{- $countParams := dict "categories" "categoryCount" "tags" "tagCount" "series" "seriesCount" }}
                {{- $param := default "" (index $countParams $key) }}
                {{- $taxonomyCount := default 10 ($.Site.Param $param) }}
                {{- if $taxonomyCount }}
                    {{- with $value.ByCount }}
                    <li class="nav-item" role="presentation">
                        <button class="nav-link{{ if eq $counter 0 }} active{{ end }}" id="taxonomy{{ $key | title }}Tab" data-bs-toggle="tab" data-bs-target="#taxonomy{{ $key | title }}" 
                            type="button" role="tab" aria-controls="taxonomy{{ $key | title }}" aria-selected="true">
                            {{ i18n $key }}
                        </button>
                    </li>
                    {{- $counter = add $counter 1 }}
                    {{- end }}
                {{- end }}
                {{- end }}
                {{- end }}
                {{- end }}
                {{- if $archives }}
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="taxonomyArchivesTab" data-bs-toggle="tab" data-bs-target="#taxonomyArchives" 
                        type="button" role="tab" aria-controls="taxonomyArchives" aria-selected="true">
                        {{ i18n "archives" }}
                    </button>
                </li>
                {{- end }}
            </ul>
            
            <div class="tab-content mt-3">
                {{- $counter := 0 }}
                {{- range $expected := $.Site.Params.sidebarTaxonomies }}
                {{- range $key, $value := $.Site.Taxonomies }}
                {{- if eq $key $expected }}
                {{- $countParams := dict "categories" "categoryCount" "tags" "tagCount" "series" "seriesCount" }}
                {{- $param := default "" (index $countParams $key) }}
                {{- $taxonomyCount := default 10 ($.Site.Param $param) }}
                {{- if $taxonomyCount }}
                    {{- with $value.ByCount }}
                    <div class="tab-pane{{ if eq $counter 0 }} active{{ end }}" id="taxonomy{{ $key | title }}" role="tabpanel" aria-labelledby="taxonomy{{ $key | title }}Tab" tabindex="0">
                        {{- $count := 0 }}
                        {{- range . }}
                        {{- if and (lt $count $taxonomyCount) (ne .Name "") }}
                        <a href="{{ .Page.RelPermalink }}" class="btn btn-sm btn-secondary post-taxonomy ps-3 post-{{ $key | singularize }} me-2 mb-2" title="{{ .Page.Title }}">
                            {{ .Page.Title }}
                            {{- if $.Site.Params.countTaxonomyPosts }}
                            <span class="badge badge-sm text-secondary bg-white ms-1">{{ .Count }}</span>
                            {{- end }}
                        </a>
                        {{- $count = add $count 1 }}
                        {{- end }}
                        {{- end }}
                        {{- if gt (len $value) $taxonomyCount }}
                        <a href="{{ absLangURL (urlize $key) }}" class="btn btn-sm btn-secondary post-taxonomy ps-3 post-{{ $key | singularize }} me-2 mb-2" title='{{ i18n "all" | upper }}'>
                        {{ i18n "all" | upper }}
                        <span class="badge badge-sm text-secondary bg-white ms-1">{{ len $value }}</span>
                        </a>
                        {{- end }}
                    </div>
                    {{- $counter = add $counter 1 }}
                    {{- end }}
                {{- end }}
                {{- end }}
                {{- end }}
                {{- end }}
                {{- if $archives }}
                <div class="tab-pane" id="taxonomyArchives" role="tabpanel" aria-labelledby="taxonomyArchivesTab" tabindex="0">
                    {{- partial "sidebar/archive-items" . }}
                </div>
                {{- end }}
            </div>
        </div>
    </div>
</div>
{{- end -}}
